using System.Linq;
using NUnit.Framework;

namespace Solver
{
	/// <summary>
	/// Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
	/// </summary>
	public class Problem030 : SelfChecker, IProblem
	{
		public int Id
		{
			get { return 30; }
		}

		public string Solve()
		{
			var pows = Enumerable.Range(0, 10).Select(d => d * d * d * d * d).ToArray();
			var s = 0;
			for (var d0 = 0; d0 < 2; d0++)
			for (var d1 = 0; d1 < 10; d1++)
			for (var d2 = 0; d2 < 10; d2++)
			for (var d3 = 0; d3 < 10; d3++)
			for (var d4 = 0; d4 < 10; d4++)
			for (var d5 = 0; d5 < 10; d5++)
			{
				var a = pows[d0] + pows[d1] + pows[d2] + pows[d3] + pows[d4] + pows[d5];
				var b = 100000 * d0 + 10000 * d1 + 1000 * d2 + 100 * d3 + 10 * d4 + d5;
				if (a == b) s += a;
			}
			return (s - 1).ToString();
		}

		[Test]
		public override void Check()
		{
			Assert.AreEqual("443839", Solve());
		}
	}
}